package com.irdstudio.apicenter.gateway.web.controller;

import java.awt.Insets;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;

import com.irdstudio.apicenter.gateway.comm.MaccsPacket;
import com.irdstudio.apicenter.gateway.comm.MaccsPacketUtil;
import com.irdstudio.apicenter.gateway.util.PathUtil;
import com.irdstudio.apicenter.gateway.web.MaccsException;
import com.irdstudio.apicenter.gateway.web.MaccsTradeDefineVo;
import com.irdstudio.apicenter.gateway.web.view.JsonView;
import com.irdstudio.apicenter.gateway.web.view.View;

@Controller
@SuppressWarnings("unchecked")
public class CreatePDF implements IController {

	/* 文本日志输出 */
	private static final Logger logger = LoggerFactory
			.getLogger(CreatePDF.class);

	private static final long serialVersionUID = 1L;

	@Override
	public View execute(MaccsTradeDefineVo tradeDefine, MaccsPacket inPacket,
			HttpServletRequest request, HttpServletResponse response)
			throws MaccsException {
		MaccsPacket backPacket = null;
		String id_num = request.getParameter("id_num");
		String doc_type = request.getParameter("doc_type");
		String html = request.getParameter("html");

		String pdfPath = PathUtil.getProjectRootPath() + "pdf/";
		String pdfPathfile = PathUtil.getProjectRootPath() + "pdf";
		File pdfFile1 = new File(pdfPathfile);
		if  (!pdfFile1 .exists()  && !pdfFile1 .isDirectory()) {
			 pdfFile1 .mkdir();  
		}
		File pdfFile = new File(pdfPath + id_num + "_" + doc_type + ".pdf");
		
		StringReader strReader = new StringReader(html);
		try {
			backPacket = this.generatePDF_1(pdfFile, strReader);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.setStatus(HttpServletResponse.SC_OK);

		return new JsonView(request, response, backPacket);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// this.uploadFile(request,response); //上传文件
	}

	// 手动构造HTML代码
	public MaccsPacket generatePDF_1(File outputPDFFile, StringReader strReader)
			throws Exception {
		String error = "";
		MaccsPacket backPacket = null;
		FileOutputStream fos =null;
		try {

			fos = new FileOutputStream(outputPDFFile);
			PD4ML pd4ml = new PD4ML();
			pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
			pd4ml.setHtmlWidth(950);
			pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
			pd4ml.useTTF("java:fonts", true);
			pd4ml.setDefaultTTFs("KaiTi", "KaiTi", "KaiTi");
			pd4ml.enableDebugInfo();
			pd4ml.render(strReader, fos);
		} catch (Exception e) {
			e.printStackTrace();
			error = "生成PDF出现错误：" + e.getMessage();
		} finally {
			try {
				if (fos != null) {
					fos.close();
					fos=null;
				}
			} catch (Exception e2) {
			}
			if (!"".equals(error)) {
				backPacket = MaccsPacketUtil.makeFailurePacket(error);
			} else {
				backPacket = MaccsPacketUtil
						.makeSimpleSuccessPacket("生成PDF到MACCS前置成功！");

			}
		}
		return backPacket;
	}

	

}
